Skip to main content
Version: 10.15.0

SplitByFormatter

The SplitByFormatter class enables conditional formatting based on the value of a split-by field. This allows you to apply different formatters to different categories or groups within your data, making it ideal for scenarios where different measures require different formatting rules when using measure names/measure values.

Constructor

new SplitByFormatter(options)

Parameters

PropertyTypeRequiredDescription
optionsSplitByFormatterOptionstrueConfiguration specifying the split field and value-specific formatters

Options

{
  splitByField: string,
  values: {
    [value: string]: PlainFormatter | undefined
  }
}
OptionTypeRequiredDefaultDescription
splitByFieldstringtrue''The name of the field whose values determine which formatter to apply
valuesObjecttrueMap of field values to their corresponding formatters

Basic Usage

const splitFormatter = new SplitByFormatter({
  splitByField: 'Product Category',
  values: {
    'Electronics': new NumberFormatter('currency', { currency: 'USD' }),
    'Food': new NumberFormatter('decimal', { decimalDigits: 2 }),
    'Clothing': new NumberFormatter('currency', { currency: 'USD', decimalDigits: 0 })
  }
});

// The formatter will apply different formatting based on the category
// For an Electronics row: $1,234.56
// For a Food row: 1,234.56
// For a Clothing row: $1,235

Methods

format()

Formats a value based on the split-by field value in the row data.

format(value: MuzeDatum, rowData: RowData, defaultFormattedValue?: string): string

Parameters:

PropertyTypeRequiredDescription
valueMuzeDatumtrueThe value to format
rowDataRowDatatrueThe complete row data containing the split-by field
defaultFormattedValuestringfalseFallback value if no formatter matches

Returns: String - The formatted value

formatterFunc()

Returns a formatter function for use with Muze visualizations.

formatterFunc(): (dataInfo: MuzeType) => string

Returns: Function - A formatter function that can be passed to Muze configuration

The returned function automatically extracts row data from the Muze data context and applies the appropriate formatter.